/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.uct.service.plugins  
 */
package com.apache.uct.service.plugins;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.uct.common.entity.Lock;

/**
 * description:  获取资源列表插件
 * @author dwusoft 创建时间：2015-3-26  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class LockListPlugin implements PluginConnector {

	private Logger log = Logger.getLogger(LockListPlugin.class);
	private IDao lockDao;

	/**
	 * TODO 获取资源列表插件. <br> 
	 * 参数说明:<br>
	 * lock属性:按资源属性查询资源列表;<br>
	 * 分页设置:<br>
	 * pageSize:每页记录数<br>
	 * pageIndex:当前页码
	 */
	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();
		try {
			result.setResult("true");
			MethodParam param = setMethodParams(vo, 2);
			if (Validator.isEmpty(param)) {
				result.setMessage("查询条件为空或不存在相关信息");
				return result;
			}
			int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "0"));
			int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "0"));
			if (pageSize == 0 && pageIndex == 0) {
				param.setOrderby(" order_num asc");
				result.setEntity(lockDao.select(param));
			} else {
				param.setPageIndex(pageIndex);
				param.setPageSize(pageSize);
				param.setOrderby(" order_num asc");
				Page page = lockDao.pageSelect(param);
				result.setEntity(page);
			}
		} catch (Exception e) {
			result.setResult("false");
			log.error("执行机构查询失败 ", e);
			result.setMessage("机构查询失败！");
		}
		return result;
	}

	private MethodParam setMethodParams(ParamsVo vo, int type) {
		StringBuffer sb = new StringBuffer(lockDao.getSql(type));
		MethodParam param = new MethodParam("ByProperty", "", "", Lock.class.getName());
		String str = buildParams(param, vo);
		if (Validator.isNull(str)) {
			return null;
		}
		sb.append(str);

		sb.append(" order by orderNum");
		param.setSqlStr(sb.toString());
		return param;
	}

	private String buildParams(MethodParam param, ParamsVo vo) {
		StringBuffer sb = new StringBuffer();
		if (Validator.isNotNull(String.valueOf(vo.getParams("lockEname")))) {
			sb.append(" and lockEname like :lockEname");
			param.setParams("lockEname", "'%" + String.valueOf(vo.getParams("lockEname")) + "%'");
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("lockCname")))) {
			sb.append(" and lockCname like :lockCname");
			param.setParams("lockCname", "'%" + String.valueOf(vo.getParams("lockCname")) + "%'");
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("lockPath")))) {
			sb.append(" and lockPath like :lockPath");
			param.setParams("lockPath", "'%" + String.valueOf(vo.getParams("lockPath")) + "%'");
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("lockType")))) {
			sb.append(" and lockType = :lockType");
			param.setParams("lockType", String.valueOf(vo.getParams("lockType")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("actId")))) {
			sb.append(" and actId = :actId");
			param.setParams("actId", String.valueOf(vo.getParams("actId")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("actFullEname")))) {
			sb.append(" and actFullEname = :actFullEname");
			param.setParams("actFullEname", String.valueOf(vo.getParams("actFullEname")));
		}
		sb.append(" and lockStatus = :lockStatus");
		param.setParams("lockStatus", "1");
		if (Validator.isNotNull(String.valueOf(vo.getParams("lockRemark")))) {
			sb.append(" and lockRemark = :lockRemark");
			param.setParams("lockRemark", String.valueOf(vo.getParams("lockRemark")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("sysId")))) {
			sb.append(" and sysId = :sysId");
			param.setParams("sysId", String.valueOf(vo.getParams("sysId")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("sysCname")))) {
			sb.append(" and sysCname = :sysCname");
			param.setParams("sysCname", String.valueOf(vo.getParams("sysCname")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("sysEname")))) {
			sb.append(" and sysEname = :sysEname");
			param.setParams("sysEname", String.valueOf(vo.getParams("sysEname")));
		}
		return sb.toString();
	}

	public void setLockDao(IDao lockDao) {
		this.lockDao = lockDao;
	}
}
